package com.example.mybatis.config;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;

/**
 * 异常统一处理
 */
@ControllerAdvice   //标识为控制器增强类
@ResponseBody
public class ErrorAdvice {
    //添加异常统一处理方法
    @ExceptionHandler(NullPointerException.class)
    public HashMap<String,Object> nullException(NullPointerException e){
        HashMap<String,Object> result = new HashMap<>();
        result.put("succ",200);
        result.put("state",-1);
        result.put("message","空指针异常");
        return result;
    }
}
